(function (factory) {
    typeof define === 'function' && define.amd ? define(factory) :
    factory();
}((function () { 'use strict';

    var Error = {
        init: function init(cb) {
            var errorfn = window.onerror;
            window.onerror = function (message, source, lineno, colno, error) {
                var errorInfo = callbackError(error);
                errorInfo.type = 'error';
                cb(errorInfo);
                errorfn && errorfn.apply(window,arguments);
            };
            
        }
    };

    function callbackError(errorObj){
        var assign;

        // 浏览器兼容
        var col = errorObj.column || errorObj;
        var row = errorObj.line || errorObj;
        var errorType = errorObj.name;
        var message = errorObj.message;
        var stack = errorObj.stack;
        var stackCol = null;
        var stackRow = null;
        var url = '';
        if(stack){
            var matcher = stack.match(/https?:\/\/[^\n]+/);
            var firstStack = matcher ? matcher[0] : '';
            var regCheck = /https?:\/\/(\S)*\/\.js?/;
            if(regCheck.test(firstStack)){
                url = firstStack.match(regCheck)[0];
            }
            var postStack = firstStack.match(/:(\d+):(\d+)/);
            if(postStack && postStack.length >= 3){
                (assign = postStack, stackCol = assign[1], stackRow = assign[2]);
            }
        }
        return {
            content:stack,
            col: Number( stackCol || col),
            row: Number(  stackRow|| row),
            errorType: errorType,message: message,url: url
        }
    }

    // Performance.init(res => {
    //     console.log(res)
    // })

    // Resource.init(res=>{
    //     console.log(res)
    // })

    // XHRHook.init((xhInfo)=>{
    //     console.log(xhInfo)
    // })

    Error.init(function (res){
        console.log(res);
    });

    // Bench.init(res=>{
    //     console.log(res)
    // })

})));
